package com.lh.blogCommon.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.apache.ibatis.reflection.MetaObject;
import org.joda.time.DateTime;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.Date;

//创建时间 2021/3/16
//创建人 lh
@EnableTransactionManagement//事务
@Configuration
@MapperScan("com.lh.*.mapper")
public class MybatisPlusConfig implements MetaObjectHandler {
    /*
      分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor(){
        return new PaginationInterceptor();
    }

    @Override
    public void insertFill(MetaObject metaObject) {
        DateTime dateTime = new DateTime(new Date());
        String fieldVal = dateTime.toString("yyyy-MM-dd");
        this.setFieldValByName("creationTime", fieldVal,metaObject);
        this.setFieldValByName("modificationTime",fieldVal,metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        DateTime dateTime = new DateTime(new Date());
        String fieldVal = dateTime.toString("yyyy-MM-dd");
        this.setFieldValByName("modificationTime",fieldVal,metaObject);
    }
}
